<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use EasyWeChat\Foundation\Application;
class UserController extends Controller
{
    //
    private $wechat;

    //通过依赖注入,初始化配置,Application 引用了 EasyWecaht的Foundation\Application;
    public function __construct(Application $application)
    {
        $this->wechat = $application;
    }

    //获取公众号的用户列表
    public function index()
    {
        $user = $this->wechat->user->lists();

        return $user;
    }

    //获取单个用户的信息
    public function show($id)
    {
        $user = $this->wechat->user->get($id);

        return $user;
    }

    //修改某个用户的备注
    public function create($id)
    {
        $this->wechat->user->remark($id, '大神级人物');

        return 'ok';
    }

    //群发消息
    public function send()
    {
       $this->wechat->broadcast->sendText('内容', [$openid1, $openid2]);
       return 'ok';
    }

    //获取全部模板消息
    public function get()
    {
       $res = $this->wechat->notice->getPrivateTemplates();
       return $res;
    }

    //发送模板消息,如果需要发送给多个用户,可循环发送
   /* 模板样例
    {{ first.DATA }}
    商品明细：
    名称：{{ name.DATA }}
    价格：{{ price.DATA }}
    {{ remark.DATA }}*/
    public function sms()
    {
       $url = 'www.baidu.com';
      $data = array(
     "first"  => "恭喜你购买成功！",
     "name"   => "巧克力",
     "price"  => "39.8元",
     "remark" => "欢迎再次购买！",
    );
       $notice  = $this->wechat->notice;
       $messageId = $notice->to($openid)->uses($templateId)->andUrl($url)->data($data)->send();
      return 'ok';
    }
}
